package com.store.app.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.BookedViewPagerAdapter;
import com.store.app.bean.CouponUpdateBean;
import com.store.app.fragment.TheOrderBuyFragment;
import com.store.app.fragment.TheOrderFreeFragment;
import com.store.app.fragment.TheOrderHPTFragment;
import com.store.app.fragment.TheOrderSellFragment;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 我的订单
 */
public class TheOrderActivity extends FragmentActivity implements View.OnClickListener {
    public List<Fragment> fragments = new ArrayList<Fragment>();
    //	private RadioGroup radioGroup;
    private TextView rb1, rb2, rb3;
    private TextView radioGroup_titlebar_1, radioGroup_titlebar_2, radioGroup_titlebar_3,radioGroup_titlebar_4;
    private TextView title;
    private ViewPager viewpager;
    private String showFragmentIndex = "0";
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tvSellNumber;

    private View tv_Right1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        try {
            if (getIntent() != null) {
                //初始化跳转到哪个Fragment
                showFragmentIndex = getIntent().getStringExtra("showFragmentIndex");
                if (showFragmentIndex == null) {
                    showFragmentIndex = "0";
                }
                Log.v("zyl", "接手到的showFragmentIndex：" + showFragmentIndex);
            }
        } catch (Exception e) {
        }
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_booked);
        findView();
        setListener();
        initData();

        if ("0".equals(showFragmentIndex)) {
            viewpager.setCurrentItem(0);
        } else if ("1".equals(showFragmentIndex)) {
            viewpager.setCurrentItem(1);
        } else if ("2".equals(showFragmentIndex)) {
            viewpager.setCurrentItem(2);
        } else if("3".equals(showFragmentIndex)){
            viewpager.setCurrentItem(3);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void setListener() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         *
         */

        //超级返帮助按钮,加载h5网页
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//                Intent intent = new Intent();
//                intent.setClass(TheOrderActivity.this, WebViewTGActivity.class);
//                intent.putExtra("title", "帮助");
//                intent.putExtra("url", SettingData.SUPER_HELP);
//                startActivity(intent);
                Intent intent = new Intent(TheOrderActivity.this, AdviceActivity.class);
                startActivity(intent);

            }
        });

    }

    private void findView() {
//		radioGroup = (RadioGroup) findViewById(R.id.booked_rg);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				switch (checkedId) {
//					case R.id.booked_rb1:
//						viewpager.setCurrentItem(0);
//						break;
//					case R.id.booked_rb2:
//						viewpager.setCurrentItem(1);
//						break;
//					case R.id.booked_rb3:
//						viewpager.setCurrentItem(2);
//						break;
//					default:
//						break;
//				}
//
//			}
//		});

        viewpager = (ViewPager) findViewById(R.id.booked_viewpager);
        radioGroup_titlebar_1 = (TextView) findViewById(R.id.booked_rb1_titleck1);
        radioGroup_titlebar_2 = (TextView) findViewById(R.id.booked_rb2_titleck2);
        radioGroup_titlebar_3 = (TextView) findViewById(R.id.booked_rb3_titleck3);
        radioGroup_titlebar_4 = (TextView) findViewById(R.id.booked_rb4_titleck4);


        tv_Right1 = findViewById(R.id.tv_right1);
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.backfankui);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
        tvSellNumber = (TextView) findViewById(R.id.tv_sell_number);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("订单");
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        if (tvSellNumber.getText().toString().trim().equals("0")) {
            tvSellNumber.setVisibility(View.GONE);
        }
    }

    private void refreshUITab(String index) {
        if (index.equals("0")) {
            tv1.setTextColor(Color.parseColor("#f96116"));
            tv2.setTextColor(Color.parseColor("#333333"));
            tv3.setTextColor(Color.parseColor("#333333"));
            tv4.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_4
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
        } else if (index.equals("1")) {
            tv2.setTextColor(Color.parseColor("#f96116"));
            tv1.setTextColor(Color.parseColor("#333333"));
            tv3.setTextColor(Color.parseColor("#333333"));
            tv4.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_4
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
        } else if (index.equals("2")) {
            tv3.setTextColor(Color.parseColor("#f96116"));
            tv1.setTextColor(Color.parseColor("#333333"));
            tv2.setTextColor(Color.parseColor("#333333"));
            tv4.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
            radioGroup_titlebar_4
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
        }else if (index.equals("3")) {
            tv4.setTextColor(Color.parseColor("#f96116"));
            tv1.setTextColor(Color.parseColor("#333333"));
            tv2.setTextColor(Color.parseColor("#333333"));
            tv3.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_3
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_4
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
        }
    }

    private void initData() {
        fragments.add(new TheOrderSellFragment());
        fragments.add(new TheOrderBuyFragment());
        fragments.add(new TheOrderFreeFragment());
        fragments.add(new TheOrderHPTFragment());
        BookedViewPagerAdapter disfratab = new BookedViewPagerAdapter(getSupportFragmentManager(), viewpager, fragments);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshUITab(position + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        refreshUITab(showFragmentIndex);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:
                showFragmentIndex = "0";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv2:
                showFragmentIndex = "1";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(1);
                break;
            case R.id.tv3:
                showFragmentIndex = "2";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(2);
                break;
            case R.id.tv4:
                showFragmentIndex = "3";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(3);
                break;
        }
    }

    public void onEvent(CouponUpdateBean event) {
        Log.v("zyl", "onEvent");
        //卖单数据修改
        if (event.type == 99) {
            if (event.number.equals("0")) {
                tvSellNumber.setVisibility(View.GONE);
            } else {
                tvSellNumber.setVisibility(View.VISIBLE);
                tvSellNumber.setText(event.number);
            }
        }
    }
}